package dao;

import model.Record;
import java.util.*;

public class RecordDAO {
    private static List<Record> records = new ArrayList<>();
    private static int nextId = 1;

    public static void addRecord(Record record) {
        record.setId(nextId++);
        records.add(record);
    }

    public static List<Record> getAllRecords() {
        return new ArrayList<>(records);
    }

    public static List<Record> getRecordsByStudentId(int studentId) {
        List<Record> studentRecords = new ArrayList<>();
        for (Record record : records) {
            if (record.getStudentId() == studentId) {
                studentRecords.add(record);
            }
        }
        return studentRecords;
    }

    public static List<Record> getRecordsByGroupId(int groupId) {
        List<Record> groupRecords = new ArrayList<>();
        for (Record record : records) {
            if (record.getGroupId() == groupId) {
                groupRecords.add(record);
            }
        }
        return groupRecords;
    }

    public static double getAverageScoreByGroupId(int groupId) {
        List<Record> groupRecords = getRecordsByGroupId(groupId);
        if (groupRecords.isEmpty()) return 0;
        
        int totalScore = 0;
        for (Record record : groupRecords) {
            totalScore += record.getScore();
        }
        return (double) totalScore / groupRecords.size();
    }

    public static void updateRecord(Record record) {
        for (int i = 0; i < records.size(); i++) {
            if (records.get(i).getId() == record.getId()) {
                records.set(i, record);
                break;
            }
        }
    }

    public static void deleteRecord(int id) {
        records.removeIf(r -> r.getId() == id);
    }

    public static Record getRecordById(int id) {
        return records.stream()
                .filter(r -> r.getId() == id)
                .findFirst()
                .orElse(null);
    }
} 